//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class TaxByCountryDBManager
    {
        #region Methods
        public static void DeleteTaxByCountry(int TaxByCountryID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryDelete");
            db.AddInParameter(dbCommand, "TaxByCountryID", DbType.Int32, TaxByCountryID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static TaxByCountry GetByTaxByCountryID(int TaxByCountryID)
        {
            TaxByCountry taxByCountry = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "TaxByCountryID", DbType.Int32, TaxByCountryID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    taxByCountry = new TaxByCountry();
                    taxByCountry.TaxByCountryID = NopDataHelper.GetInt(dataReader, "TaxByCountryID");
                    taxByCountry.TaxCategoryID = NopDataHelper.GetInt(dataReader, "TaxCategoryID");
                    taxByCountry.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    taxByCountry.Percentage = NopDataHelper.GetDecimal(dataReader, "Percentage");
                }
            }
            return taxByCountry;
        }

        public static TaxByCountryCollection GetAllByCountryID(int CountryID)
        {
            TaxByCountryCollection taxByCountryCollection = new TaxByCountryCollection();
            if (CountryID == 0)
                return taxByCountryCollection;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryLoadAllByCountryID");
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    TaxByCountry taxByCountry = new TaxByCountry();
                    taxByCountry.TaxByCountryID = NopDataHelper.GetInt(dataReader, "TaxByCountryID");
                    taxByCountry.TaxCategoryID = NopDataHelper.GetInt(dataReader, "TaxCategoryID");
                    taxByCountry.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    taxByCountry.Percentage = NopDataHelper.GetDecimal(dataReader, "Percentage");
                    taxByCountryCollection.Add(taxByCountry);
                }
            }
            return taxByCountryCollection;
        }

        public static TaxByCountry InsertTaxByCountry(int TaxCategoryID, int CountryID, decimal Percentage)
        {
            TaxByCountry taxByCountry = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryInsert");
            db.AddOutParameter(dbCommand, "TaxByCountryID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "TaxCategoryID", DbType.Int32, TaxCategoryID);
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            db.AddInParameter(dbCommand, "Percentage", DbType.Decimal, Percentage);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int TaxByCountryID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@TaxByCountryID"));
                taxByCountry = GetByTaxByCountryID(TaxByCountryID);
            }
            return taxByCountry;
        }

        public static TaxByCountry UpdateTaxByCountry(int TaxByCountryID, int TaxCategoryID, int CountryID, decimal Percentage)
        {
            TaxByCountry taxByCountry = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryUpdate");
            db.AddInParameter(dbCommand, "TaxByCountryID", DbType.Int32, TaxByCountryID);
            db.AddInParameter(dbCommand, "TaxCategoryID", DbType.Int32, TaxCategoryID);
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            db.AddInParameter(dbCommand, "Percentage", DbType.Decimal, Percentage);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                taxByCountry = GetByTaxByCountryID(TaxByCountryID);

            return taxByCountry;
        }
        #endregion
    }
}
